<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue</title>
  <script src="./js/vue.js"></script>
  <style>
    .error {
      color: red;
    }
  </style>
</head>

<body>
  <div id="app">
    <input type="text" v-focus v-model="title">
  </div>

  <script>

    let vm = new Vue({
      el: '#app',
      data: {
        title: ''
      },
      // 局部指令的配置
      directives: {
        // focus: {
        //   // 5个钩子函数
        //   inserted(el, binds) {
        //     el.focus()
        //     /* let tipDom = document.createElement('span')
        //     tipDom.innerHTML = '超出限制的长度'
        //     tipDom.className = 'error'
        //     el.parentNode.appendChild(tipDom) */
        //   }
        // }

        focus(el, binds) {
          // 局部指令中this也不是指向当前vue实例对象
          // console.log(this)
          if (el.value.length > 4) {
            if (!el.nextSibling) {
              let tipDom = document.createElement('span')
              tipDom.innerHTML = '超出限制的长度'
              tipDom.className = 'error'
              el.parentNode.appendChild(tipDom)
            }
          } else {
            el.nextSibling && el.nextSibling.remove()
          }
        }
      },
      methods: {
      }
    })
  </script>
</body>

</html>